﻿// -----------------------------------------------------------------------
// <copyright file="RenameForm.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.CommonForms
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// Rename a file
    /// </summary>
    public partial class RenameForm : Form
    {
        /// <summary>Stores the old file name</summary>
        private string oldFilePath;

        /// <summary>Stores the extension of our file, used to reconstruct the file name</summary>
        private string fileExtension;

        /// <summary>
        /// Initializes a new instance of the RenameForm class.
        /// </summary>
        /// <param name="filePath">The full path (including file name)</param>
        public RenameForm(string filePath)
        {
            try
            {
                this.InitializeComponent();
                this.oldFilePath = filePath;
                this.lblTitle.Text = "Rename " + System.IO.Path.GetFileNameWithoutExtension(filePath) + " to:";
                this.fileExtension = System.IO.Path.GetExtension(filePath);
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Cancel renaming
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Rename the file
        /// </summary>
        /// <param name="sender">Object sender</param>
        /// <param name="e">Event args</param>
        private void ButtonOk_Click(object sender, EventArgs e)
        {
            try
            {
                string path = System.IO.Path.GetDirectoryName(this.oldFilePath);

                if (this.newName.Text != string.Empty)
                {
                    System.IO.File.Move(this.oldFilePath, path + "\\" + this.newName.Text + this.fileExtension);
                }

                this.Close();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }
    }
}
